package o;

import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class ui<K extends ul, V> {
    private final Map<K, a<K, V>> ˋ = new HashMap();
    private final a<K, V> ॱ = new a();

    static class a<K, V> {
        final K ˊ;
        private List<V> ˎ;
        a<K, V> ˏ;
        a<K, V> ॱ;

        a() {
            this(null);
        }

        a(K k) {
            this.ॱ = this;
            this.ˏ = this;
            this.ˊ = k;
        }

        @Nullable
        public V ॱ() {
            int ˋ = ˋ();
            return ˋ > 0 ? this.ˎ.remove(ˋ - 1) : null;
        }

        public int ˋ() {
            return this.ˎ != null ? this.ˎ.size() : 0;
        }

        public void ˏ(V v) {
            if (this.ˎ == null) {
                this.ˎ = new ArrayList();
            }
            this.ˎ.add(v);
        }
    }

    ui() {
    }

    public void ˋ(K k, V v) {
        a aVar = (a) this.ˋ.get(k);
        if (aVar == null) {
            aVar = new a(k);
            ॱ(aVar);
            this.ˋ.put(k, aVar);
        } else {
            k.ˎ();
        }
        aVar.ˏ(v);
    }

    @Nullable
    public V ˋ(K k) {
        a aVar = (a) this.ˋ.get(k);
        if (aVar == null) {
            aVar = new a(k);
            this.ˋ.put(k, aVar);
        } else {
            k.ˎ();
        }
        ˏ(aVar);
        return aVar.ॱ();
    }

    @Nullable
    public V ˋ() {
        for (a aVar = this.ॱ.ॱ; !aVar.equals(this.ॱ); aVar = aVar.ॱ) {
            V ॱ = aVar.ॱ();
            if (ॱ != null) {
                return ॱ;
            }
            ˋ(aVar);
            this.ˋ.remove(aVar.ˊ);
            ((ul) aVar.ˊ).ˎ();
        }
        return null;
    }

    public String toString() {
        StringBuilder stringBuilder = new StringBuilder("GroupedLinkedMap( ");
        Object obj = null;
        for (a aVar = this.ॱ.ˏ; !aVar.equals(this.ॱ); aVar = aVar.ˏ) {
            obj = 1;
            stringBuilder.append('{').append(aVar.ˊ).append(':').append(aVar.ˋ()).append("}, ");
        }
        if (obj != null) {
            stringBuilder.delete(stringBuilder.length() - 2, stringBuilder.length());
        }
        return stringBuilder.append(" )").toString();
    }

    private void ˏ(a<K, V> aVar) {
        ˋ((a) aVar);
        aVar.ॱ = this.ॱ;
        aVar.ˏ = this.ॱ.ˏ;
        ˎ(aVar);
    }

    private void ॱ(a<K, V> aVar) {
        ˋ((a) aVar);
        aVar.ॱ = this.ॱ.ॱ;
        aVar.ˏ = this.ॱ;
        ˎ(aVar);
    }

    private static <K, V> void ˎ(a<K, V> aVar) {
        aVar.ˏ.ॱ = aVar;
        aVar.ॱ.ˏ = aVar;
    }

    private static <K, V> void ˋ(a<K, V> aVar) {
        aVar.ॱ.ˏ = aVar.ˏ;
        aVar.ˏ.ॱ = aVar.ॱ;
    }
}
